11. 盛最多水的容器
为保证权益,题目请参考 11. 盛最多水的容器(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
int maxArea(vector<int> &height)
{
int max_area = 0;
int left = 0;
int right = height.size() - 1;
while (left < right)
{
if (height[left] < height[right])
{
max_area = max(max_area, (right - left) * (height[left]));
left++;
}else
{
max_area = max(max_area, (right - left) * (height[right]));
right--;
}
}
return max_area;
}
};
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Python
python
from typing import List
class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height)-1
ans = -1
while l < r:
ans = max(ans, (r - l)*min(height[l],height[r]))
if height[l] <= height[r]:
l += 1
else:
r -= 1
return ans
if __name__ == "__main__":
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21